/**
 * 新增偏激品牌
 */
import { ApiRequest } from '@api/basePage';
export default {
    data () {
        return {
            sLoading: false,
            uuid: '',
            brandImageKey1: [], // 正方形图标回显地址
            brandImageKey2: [], // 长方形图标回显地址
            brandImageKey3:[],//厂家Logo
            brandFormData: {
                brandName: '',
                brandEnName: '',
                note: '',
                imageKey1: '',
                imageKey2: '',
                factoryLogo:'',
                insideLevel: '',
                outsideLevel: '',
                series: '', // 系列
                parentUuid: '',
                brandState: '1',
                factoryInfluenceMore:'1',
            },
            resetBrandFormData: {},
            brandFormRules: {
                brandName: [
                    { required: true, message: '请填写品牌名称', trigger: 'change' },
                    { type: 'string', min: 1, message: '品牌名称不得少于一个字符', trigger: 'blur' }
                ],
                brandEnName: [
                    { required: true, message: '请填写品牌编号', trigger: 'change' },
                    { type: 'string', min: 1, message: '品牌编号不得少于一个字符', trigger: 'blur' }
                ],
                imageKey2: [
                    { required: true, message: '请上传长方形图', trigger: 'change' }
                ],
                imageKey1: [
                    { required: true, message: '请上传正方形图', trigger: 'change' }
                ],
                factoryLogo: [
                    { required: true, message: '请上传厂家Logo', trigger: 'change' }
                ],
                note: [
                    { required: true, message: '请填写品牌描述', trigger: 'change' },
                    { type: 'string', min: 5, message: '品牌描述不得少于5个字符', trigger: 'blur' }
                ]
            }
        }
    },
    created () {
        this.uuid = this.$route.query.uuid;
        this.resetBrandFormData = JSON.parse(JSON.stringify(this.brandFormData));
    },
    methods: {
        // 上传正方形图片
        changeImageKey1 (imgArr) {
            let bool = true
            imgArr.forEach((item, index) => {
                if (index === 0) {
                    this.brandFormData.imageKey1 = item.response.retData;
                    // 上传图片时，不进行置空
                    bool = false
                }
            })
            if (bool) {
                // 删除图片时置空
                this.brandFormData.imageKey1 = '';
            }
        },
        // 上传长方形图片
        changeImageKey2 (imgArr) {
            let bool = true
            imgArr.forEach((item, index) => {
                if (index === 0) {
                    // 上传图片时，不进行置空
                    bool = false
                    this.brandFormData.imageKey2 = item.response.retData;
                }
            })
            if (bool) {
                // 删除图片时置空
                this.brandFormData.imageKey2 = '';
            }
        },
        // 上传厂家logo
        changeImageKey3 (imgArr) {
            let bool = true
            imgArr.forEach((item, index) => {
                if (index === 0) {
                    // 上传图片时，不进行置空
                    bool = false
                    this.brandFormData.factoryLogo = item.response.retData;
                }
            })
            if (bool) {
                // 删除图片时置空
                this.brandFormData.factoryLogo = '';
            }
        },
        // 提交
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (this.brandFormData.imageKey1 === '') {
                    this.$Message.error('请上传正方形图');
                    return
                }

                if (this.brandFormData.imageKey2 === '') {
                    this.$Message.error('请上传长方形图');
                    return
                }

                if (this.brandFormData.factoryLogo === '') {
                    this.$Message.error('请上传厂家Logo');
                    return
                }
                let msgTxt = '新增';
                if (valid) {
                    if (this.sLoading) {
                        return false;
                    }
                    this.sLoading = true;
                    let data = {
                        uuid: this.uuid,
                        ...this.brandFormData
                    }
                    if (this.uuid) {
                        msgTxt = '更新'
                    }
                    // 开始请求会员信息
                    ApiRequest(this.requestApi.saveBrandInfo.url, this.requestApi.saveBrandInfo.type, data).then(res => {
                        if (res.retStatus === '1') {
                            this.$Message.success('品牌' + msgTxt + '成功!');
                            this.$router.go(-1)
                        }
                        this.sLoading = false;
                    })
                } else {
                    this.$Message.error('品牌' + msgTxt + '失败，请稍后重试!');
                }
            })
        },
        // 清空
        handleReset (name) {
            this.brandFormData = JSON.parse(JSON.stringify(this.resetBrandFormData));
            // this.$refs[name].resetFields();
        }
    }
}
